<?php
namespace app\resources\model;
use think\Model;
use hg\apidoc\annotation\WithoutField;

class BaseModel extends Model
{
	//隐藏字段
	protected $hidden = ["is_del"];

    /**
     * 自动赋值
     */
    public function fill(array $params) {
        $that =  $this;
        $fields = array_keys($that->getFields());
        foreach($fields as $field){
            foreach($params as $key => $param){
                if($field == $key){
                    $that->$field = $param;
                }
            }
        }
        return $that;
    }

    /**
     * 根据查询条件获取当前模型的 分页列表
     */
    public function getList($where=[],$page=1,$limit=20,$field=[],$order=[]) {
		$page = request()->param("pageIndex",$page);
        $limit =  request()->param("pageSize",$limit);
        $list = $this->where($where)
        	->order($order)
        	->field($field)
        	->withoutField('add_time')
			->paginate(['page' => $page,'list_rows'=> $limit])
            ->toArray();
        return $list;
    }

    /**
     * @withoutField("add_time,create_time,update_time")
     */
    public function getInfoById($id){
        $info = $this->where('id',$id)
        	->withoutField(['add_time'])
        	->find();
        return $info;
    }

    /**
     * 新增一条数据
     * @param $data
     * @return BaseModel|array|Model
     */
    public function createInfo($data){
        $res = $this->create($data);
        return $res;
    }

    /**
     * 按id 更新数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateInfo($id, $data,$field = true) {
        $res = $this->where(["id" => $id])->field($field)->save($data);
        if ($res){
            return true;
        }else{
            return false;
        }
    }


}